home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Tools & Goodies / ODFx / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-08-26  |  13.5 KB  |  475 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "ODFx.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. #ifndef FWUTIL_H
  21. #include "FWUtil.h"
  22. #endif
  23.  
  24. #ifndef FWKIND_H
  25. #include "FWKind.h"
  26. #endif
  27.  
  28. //========================================================================================
  29. //    Runtime info
  30. //========================================================================================
  31.  
  32. #ifdef FW_BUILD_MAC
  33. #pragma segment odfx
  34. #endif
  35.  
  36. FW_DEFINE_AUTO(CODFxContent)
  37.  
  38. //========================================================================================
  39. //    CODFxContent class
  40. //========================================================================================
  41.  
  42. static FW_CColor *PrivGetInitialColors()
  43. {
  44.     static FW_CColor colors[9] = {
  45.         FW_kRGBRed,        // top left
  46.         FW_kRGBBlack,    // top center
  47.         FW_kRGBGreen,    // top right
  48.         FW_kRGBYellow,    // left center
  49.         FW_kRGBGreen,    // center
  50.         FW_kRGBBlue,    // right center
  51.         FW_kRGBYellow,    // bottom left
  52.         FW_kRGBYellow,    // bottom center
  53.         FW_kRGBWhite    // bottom right
  54.     };
  55.     return colors;
  56. }
  57.  
  58. CODFxContent::ODFXContentStruct::ODFXContentStruct(Boolean initColors)
  59. :    fVisibleGridLines(false),
  60.     fUnused(false),
  61.     fPixPerRow(kMidPixPerRow),
  62.     fNumColors(9)
  63. {
  64.     if(initColors)
  65.         for (short i = 0; i < 9; i++)
  66.             fColors[i] = PrivGetInitialColors()[i];
  67. };
  68.  
  69. //----------------------------------------------------------------------------------------
  70. // CODFxContent constructor
  71. //----------------------------------------------------------------------------------------
  72.  
  73. CODFxContent::CODFxContent(Environment* ev, CODFxPart* part) :
  74.     FW_CContent(ev, part),
  75.     fODFxPart(part),
  76.     fPicture(),
  77.     fPictureValid(false),
  78.     fContentData(true)
  79. {
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. // CODFxContent destructor
  84. //----------------------------------------------------------------------------------------
  85.  
  86. CODFxContent::~CODFxContent()
  87. {
  88. }
  89.  
  90. //----------------------------------------------------------------------------------------
  91. // CODFxContent::GetColorData
  92. //----------------------------------------------------------------------------------------
  93.  
  94. const FW_CColor& CODFxContent::GetColorData(short n)
  95. {
  96.     return fContentData.fColors[n];
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. // CODFxContent::GetColorData
  101. //----------------------------------------------------------------------------------------
  102.  
  103. void CODFxContent::GetColorData(RegionColorArray& colors)
  104. {
  105.     for(short i = 0; i < 9; i++)
  106.         colors[i] = fContentData.fColors[i];
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // CODFxContent::SetColorData
  111. //----------------------------------------------------------------------------------------
  112.  
  113. void CODFxContent::SetColorData(Environment* ev, short n, const FW_CColor& newColor)
  114. {
  115.     fContentData.fColors[n] = newColor;
  116.     
  117.     InvalidatePicture(ev);
  118.     
  119.     fODFxPart->PartChanged(ev);
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // CODFxContent::SetColorData
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void CODFxContent::SetColorData(Environment* ev, const RegionColorArray& colors)
  127. {
  128.     for(short i = 0; i < 9; i++)
  129.         fContentData.fColors[i] = colors[i];
  130.         
  131.     InvalidatePicture(ev);
  132.     
  133.     fODFxPart->PartChanged(ev);
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // CODFxContent::GetNumColors
  138. //----------------------------------------------------------------------------------------
  139.  
  140. short CODFxContent::GetNumColors()
  141. {
  142.     return fContentData.fNumColors;
  143. }
  144.  
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // CODFxContent::SetNumColors
  148. //----------------------------------------------------------------------------------------
  149.  
  150. void CODFxContent::SetNumColors(Environment* ev, short numColors)
  151. {
  152.     if(numColors != fContentData.fNumColors)
  153.     {
  154.         fContentData.fNumColors = numColors;
  155.  
  156.         InvalidatePicture(ev);
  157.  
  158.         fODFxPart->PartChanged(ev);
  159.     }
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // CODFxContent::GetContentData
  164. //----------------------------------------------------------------------------------------
  165.  
  166. void CODFxContent::GetContentData(ODFXContentStruct& outData)
  167. {
  168.     outData = fContentData;
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // CODFxContent::SetContentData
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void CODFxContent::SetContentData(Environment* ev, const ODFXContentStruct& inData)
  176. {
  177.     fContentData = inData;
  178.     
  179.     InvalidatePicture(ev);
  180.  
  181.     fODFxPart->PartChanged(ev);
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // CODFxContent::GetShowGridLines
  186. //----------------------------------------------------------------------------------------
  187.  
  188. Boolean CODFxContent::GetShowGridLines()
  189. {
  190.     return fContentData.fVisibleGridLines;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // CODFxContent::SetShowGridLines
  195. //----------------------------------------------------------------------------------------
  196.  
  197. void CODFxContent::SetShowGridLines(Environment* ev, Boolean show)
  198. {
  199.     fContentData.fVisibleGridLines = show;
  200.     
  201.     fODFxPart->PartChanged(ev);
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // CODFxContent::GetMaxResolution
  206. //----------------------------------------------------------------------------------------
  207.  
  208. short CODFxContent::GetPixPerRow()
  209. {
  210.     return fContentData.fPixPerRow;
  211. }
  212.  
  213. //----------------------------------------------------------------------------------------
  214. // CODFxContent::SetMaxResolution
  215. //----------------------------------------------------------------------------------------
  216.  
  217. void CODFxContent::SetPixPerRow(Environment* ev, short inRes)
  218. {
  219.     inRes = (inRes / 2) * 2;
  220.     
  221.     if(inRes != fContentData.fPixPerRow)
  222.     {
  223.         fContentData.fPixPerRow = inRes;
  224.  
  225.         InvalidatePicture(ev);
  226.  
  227.         fODFxPart->PartChanged(ev);
  228.     }
  229. }
  230.  
  231.  
  232. //----------------------------------------------------------------------------------------
  233. // CODFxContent::RotateColors
  234. //----------------------------------------------------------------------------------------
  235.  
  236. void CODFxContent::RotateColors(Environment* ev)
  237. {
  238.     if(GetNumColors() == 4)
  239.     {
  240.         FW_CColor temp = fContentData.fColors[0];
  241.         
  242.         fContentData.fColors[0] = fContentData.fColors[3];
  243.  
  244.         fContentData.fColors[3] = fContentData.fColors[4];
  245.  
  246.         fContentData.fColors[4] = fContentData.fColors[1];
  247.  
  248.         fContentData.fColors[1] = temp;        
  249.     }
  250.     else
  251.     {
  252.         FW_CColor temp = fContentData.fColors[0];
  253.         
  254.         fContentData.fColors[0] = fContentData.fColors[3];
  255.  
  256.         fContentData.fColors[3] = fContentData.fColors[6];
  257.  
  258.         fContentData.fColors[6] = fContentData.fColors[7];
  259.  
  260.         fContentData.fColors[7] = fContentData.fColors[8];
  261.  
  262.         fContentData.fColors[8] = fContentData.fColors[5];
  263.  
  264.         fContentData.fColors[5] = fContentData.fColors[2];
  265.  
  266.         fContentData.fColors[2] = fContentData.fColors[1];
  267.  
  268.         fContentData.fColors[1] = temp;        
  269.     }
  270.     
  271.     InvalidatePicture(ev);
  272.  
  273.     fODFxPart->PartChanged(ev);
  274. }
  275.  
  276. //----------------------------------------------------------------------------------------
  277. // CODFxContent::ResetColorData
  278. //----------------------------------------------------------------------------------------
  279.  
  280. void CODFxContent::ResetColorData(Environment* ev)
  281. {
  282.     SetContentData(ev, ODFXContentStruct(true));
  283. }
  284.  
  285. //----------------------------------------------------------------------------------------
  286. // CODFxContent::ExternalizeKind
  287. //----------------------------------------------------------------------------------------
  288.  
  289. void CODFxContent::ExternalizeKind(Environment* ev,
  290.                                  ODStorageUnit* storageUnit,
  291.                                  FW_CKind* kind,
  292.                                  FW_StorageKinds storageKind,
  293.                                  FW_CPromise* promise,
  294.                                  FW_CCloneInfo* cloneInfo)
  295. {
  296.     FW_UNUSED(cloneInfo);
  297.     FW_UNUSED(storageKind);
  298.     FW_UNUSED(promise);
  299.     
  300.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  301.     FW_CWritableStream stream(suSink);
  302.  
  303.     if (kind->IsPartKind(ev)) 
  304.     {
  305.         stream << fContentData.fNumColors;
  306.         for(short i = 0; i < 9; i++)
  307.             stream << fContentData.fColors[i];
  308.         
  309.         stream << fContentData.fPixPerRow;
  310.         stream << fContentData.fVisibleGridLines;
  311.     }
  312.     else if (kind->IsEqual(ev, 'PICT', kODPlatformDataType))
  313.     {
  314.         FW_PlatformPict platformPict = GetPictureData(ev).GetPlatformPict();
  315.  
  316.         unsigned long pictSize = FW_CMemoryManager::GetSystemHandleSize((FW_PlatformHandle)platformPict);
  317.         
  318.         FW_CAcquireLockedSystemHandle lockedHandle((FW_PlatformHandle)platformPict);
  319.         stream.Write(lockedHandle.GetPointer(), pictSize);    
  320.     }
  321.     else if (kind->IsEqual(ev, 'PICT', kODPlatformFileType))
  322.     {
  323.         long myZero = 0;
  324.         
  325.         for(short i = 0; i < (512 / sizeof(myZero)); i++)
  326.             stream << myZero;
  327.             
  328.         FW_PlatformPict platformPict = GetPictureData(ev).GetPlatformPict();
  329.  
  330.         unsigned long pictSize = FW_CMemoryManager::GetSystemHandleSize((FW_PlatformHandle)platformPict);
  331.         
  332.         FW_CAcquireLockedSystemHandle lockedHandle((FW_PlatformHandle)platformPict);
  333.         stream.Write(lockedHandle.GetPointer(), pictSize);    
  334.     }
  335.     
  336.     // ----- Clear the end of the focused value -----
  337.     FW_SUDeleteEndOfFocusedValue(ev, storageUnit);
  338. }
  339.  
  340. //----------------------------------------------------------------------------------------
  341. // CODFxContent::InternalizeKind
  342. //----------------------------------------------------------------------------------------
  343.  
  344. FW_Boolean CODFxContent::InternalizeKind(Environment* ev,
  345.                                      ODStorageUnit* sourceSU, 
  346.                                       FW_CKind* kind,
  347.                                      FW_StorageKinds storageKind,
  348.                                      FW_CCloneInfo* cloneInfo)
  349. {
  350.     FW_UNUSED(cloneInfo);
  351.     
  352.     unsigned long size = sourceSU->GetSize(ev);
  353.     
  354.     if (kind->IsPartKind(ev))
  355.     {
  356.         FW_PStorageUnitSink suSink(ev, sourceSU, kODPropContents, kind->GetType(ev));
  357.         FW_PBufferedSink sink(ev, suSink);
  358.         FW_CReadableStream stream(sink);
  359.         
  360.         const short kOldVersionSize = 56;
  361.         const short kNewVersionSize = sizeof(fContentData);
  362.         Boolean oldVersion = sourceSU->GetSize(ev) == kOldVersionSize;
  363.         Boolean newVersion = sourceSU->GetSize(ev) == kNewVersionSize;
  364.         
  365.         short numColors;        
  366.         stream >> numColors;
  367.  
  368.         RegionColorArray colors;
  369.         for(short i = 0; i < 9; i++)
  370.             stream >> colors[i];
  371.  
  372.         if(! oldVersion)
  373.         {
  374.             short pixPerRow;        
  375.             stream >> pixPerRow;
  376.  
  377.             FW_Boolean visibleGrid;
  378.             stream >> visibleGrid;
  379.             
  380.             SetPixPerRow(ev, pixPerRow);
  381.             SetShowGridLines(ev, visibleGrid);
  382.         }
  383.  
  384.         SetNumColors(ev, numColors);
  385.         SetColorData(ev, colors);
  386.     }
  387.     else
  388.     {
  389.         FW_DEBUG_MESSAGE("CODFxContent::InternalizeKind - Unknown type");
  390.     }
  391.         
  392.     if (storageKind != FW_kPartStorage)
  393.         fODFxPart->PartChanged(ev);
  394.  
  395.     return TRUE;
  396. }
  397.  
  398. //----------------------------------------------------------------------------------------
  399. // CODFxContent::GetPictureData
  400. //----------------------------------------------------------------------------------------
  401.  
  402. FW_CPicture& CODFxContent::GetPictureData(Environment* ev)
  403. {
  404.     const short kMaxColor = GetPixPerRow();
  405.     
  406.     if(!fPictureValid)
  407.     {
  408.         FW_gWaitCursor.Select();
  409.  
  410.         // make a kMaxColor pixel tall by kMaxColor pixel wide, 24-bit direct bitmap
  411.         FW_CBitmap bits(kMaxColor, kMaxColor, 24, NULL);
  412.         {            
  413.             const short range = GetNumColors() == 9 ? kMaxColor / 2 : kMaxColor;
  414.             
  415.             // top left square if 9 colors, otherwise the whole enchilada
  416.             for(short i = 0; i < range; i++)
  417.             {
  418.                 FW_CColor colStart = FW_CColor().Blend(GetColorData((short)0), GetColorData(1), i, range - 1);
  419.                 FW_CColor colEnd = FW_CColor().Blend(GetColorData(3), GetColorData(4), i, range - 1);
  420.                 
  421.                 for(short j = 0; j < range; j++)
  422.                     bits.SetPixelColor(i, j, FW_CColor().Blend(colStart, colEnd, j, range - 1));
  423.             }
  424.             
  425.             if(range < kMaxColor)
  426.  
  427.             {
  428.                 // top right square
  429.                 for(short i = 0; i < range; i++)
  430.                 {
  431.                     FW_CColor colStart = FW_CColor().Blend(GetColorData(1), GetColorData(2), i, range - 1);
  432.                     FW_CColor colEnd = FW_CColor().Blend(GetColorData(4), GetColorData(5), i, range - 1);
  433.                     
  434.                     for(short j = 0; j < range; j++)
  435.                         bits.SetPixelColor(i + range, j, FW_CColor().Blend(colStart, colEnd, j, range - 1));
  436.                 }
  437.  
  438.                 // bottom left square
  439.                 for(short i = 0; i < range; i++)
  440.                 {
  441.                     FW_CColor colStart = FW_CColor().Blend(GetColorData(3), GetColorData(4), i, range - 1);
  442.                     FW_CColor colEnd = FW_CColor().Blend(GetColorData(6), GetColorData(7), i, range - 1);
  443.                     
  444.                     for(short j = 0; j < range; j++)
  445.                         bits.SetPixelColor(i, j + range, FW_CColor().Blend(colStart, colEnd, j, range - 1));
  446.                 }
  447.  
  448.                 // top right square
  449.                 for(short i = 0; i < range; i++)
  450.                 {
  451.                     FW_CColor colStart = FW_CColor().Blend(GetColorData(4), GetColorData(5), i, range - 1);
  452.                     FW_CColor colEnd = FW_CColor().Blend(GetColorData(7), GetColorData(8), i, range - 1);
  453.                     
  454.                     for(short j = 0; j < range; j++)
  455.                         bits.SetPixelColor(i + range, j + range, FW_CColor().Blend(colStart, colEnd, j, range - 1));
  456.                 }
  457.             }
  458.         }
  459.         
  460.         {
  461.             FW_CPictureContext picContext(ev, fPicture, FW_IntToFixed(kMaxColor), FW_IntToFixed(kMaxColor));
  462.             FW_CRect pictureBounds(FW_kFixed0, FW_kFixed0, FW_IntToFixed(kMaxColor), FW_IntToFixed(kMaxColor));
  463.             picContext.SetClipRect(pictureBounds);
  464.             FW_CBitmapShape::RenderBitmap(picContext, bits, pictureBounds, ditherCopy);
  465.         }
  466.         
  467.         fPictureValid = true;
  468.         
  469.         FW_gArrowCursor.Select();
  470.     }
  471.     
  472.     return fPicture;
  473. }
  474.  
  475.